home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / basename / basename.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-13  |  1.3 KB  |  54 lines

  1. /*
  2.  * basename.c --
  3.  *
  4.  *  Basename deletes any prefix ending with '/' from its first
  5.  *  argument, and also deletes any suffix from the first argument
  6.  *  that matches the second argument.  The resulting string is
  7.  *  printed to the standard output.
  8.  *
  9.  * Copyright 1988 Regents of the University of California
  10.  * Permission to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose and without
  12.  * fee is hereby granted, provided that the above copyright
  13.  * notice appear in all copies.  The University of California
  14.  * makes no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without
  16.  * express or implied warranty.
  17.  */
  18.  
  19. #ifndef lint
  20. static char rcsid[] = "$Header: /a/newcmds/basename/RCS/basename.c,v 1.2 88/12/13 11:05:07 rab Exp $";
  21. #endif
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26.  
  27. void
  28. main(argc, argv)
  29.     int argc;
  30.     char **argv;
  31. {
  32.     char *s;
  33.     int len;
  34.  
  35.     if (argc < 2) {
  36.     fputs("usage: basename string [suffix]\n", stderr);
  37.     exit(1);
  38.     }
  39.     if ((s = strrchr(argv[1], '/')) == NULL) {
  40.     s = argv[1];
  41.     } else {
  42.     ++s;
  43.     }
  44.     if (argc > 2) {
  45.     if ((len = strlen(s) - strlen(argv[2])) >= 0) {
  46.         if (strcmp(s + len, argv[2]) == 0)
  47.         s[len] = 0;
  48.     }
  49.     }
  50.     puts(s);
  51.     exit(0);
  52. }
  53.  
  54.